<template>
	<view class="">
		<view class="column-css">
			<mescroll-body ref="mescroll" @init="mescrollInit" @down="downCallback" @up="upCallback">
				<task-list ref="taskList" :list="dataList" :etp-name='etpName' :ship-info="shipInfo" @order-success="reloadList" @auth-success="getShipInfo" @sign-success="reloadList"></task-list>
			</mescroll-body>
		</view>
		<auth-modal ref="authModal" :ship-info="shipInfo" :show-auth="shipInfo.captainStatus === '0'"></auth-modal>
		<s-tabbar path="/pages/ship/index/task"></s-tabbar>
	</view>
</template>

<script>
	import sTabbar from '@/components/s-tabbar/s-tabbar.vue';
	import authModal from './task/authModal.vue';
	import taskList from './task/taskList.vue';
	import MescrollMixin from "@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js";
	export default {
		components: {
			sTabbar,
			authModal,
			taskList
		},
		mixins: [MescrollMixin],
		data() {
			return {
				dataList: [],
				etpName: '',
				etpId: '',
				shipInfo: {}
			}
		},
		onLoad(options) {
			this.userInfo = uni.getStorageSync(this.DATA_KEY.userInfo);
			if (options.isvo == '2') {
				let { etpId, etpName, exemptCode} = options;
				let that = this;
				that.wxRequest({
					url: `${that.API}/captainApp/isCaptainEtp?etpId=${etpId}`,
					method: 'GET',
					data: {},
					success: res => {
						if (res.data.data.flag == 1) {
							uni.showModal({
								title: '提示',
								content: '您已加入该企业！',
								success(result) {
									console.log(result)
								}
							})
						} else {
							uni.navigateTo({
								url: '/pages/ship/user/infoSet?etpId=' + etpId +
									'&etpName=' + etpName + '&exemptCode=' + exemptCode
							});
							return;
						}
					}
				});
			}
		},
		async onShow() {
			this.etpName = uni.getStorageSync(this.DATA_KEY.etpName)
			this.etpId = uni.getStorageSync(this.DATA_KEY.etpId)
			let that = this;
			if (uni.getStorageSync(this.DATA_KEY.scanUrl)) { 
				let url = uni.getStorageSync(this.DATA_KEY.scanUrl)
				if (!this.userInfo.captainPhone) {
					return;
				}
				let etpId = this.getQueryVariable(url, 'etpId');
				let etpName = this.getQueryVariable(url, 'etpName');
				let exemptCode = this.getQueryVariable(url, 'exemptCode');
				if (this.userInfo.idcard == '') {
					uni.navigateTo({
						url: '/pages/ship/user/infoSet?etpId=' + etpId +
							'&etpName=' + etpName + '&exemptCode=' + exemptCode
					});
					uni.removeStorageSync(this.DATA_KEY.scanUrl)
					return;
				}
				
				that.wxRequest({
					url: `${that.API}/captainApp/isCaptainEtp?etpId=${etpId}`,
					method: 'GET',
					data: {},
					success: res => {
						if (res.data.data.flag == 1) {
							uni.showModal({
								title: '提示',
								content: '您已加入该企业！',
								success(result) {
									console.log(result)
								}
							})
							uni.removeStorageSync(this.DATA_KEY.scanUrl)
						} else {
							uni.navigateTo({
								url: '/pages/ship/user/infoSet?etpId=' + etpId +
									'&etpName=' + etpName + '&exemptCode=' + exemptCode
							});
							uni.removeStorageSync(this.DATA_KEY.scanUrl)
							return;
						}
					}
				});
			}
			// this.$nextTick(() => {
			// 	this.$refs['authModal'].getShipInfo()
			// })
			this.$nextTick(()=>{
				this.$refs.taskList.pageShow()
			})
			this.getShipInfo()
		},
		
		methods: {
			upCallback(page) {
				let that = this;
				let pageNum = page.num;
				let pageSize = page.size;
				that.wxRequest({
					url: `${that.API}/captainApp/myTasks`,
					method: 'POST',
					data: {
						pageNum,
						pageSize,
						etpId: that.etpId,
					},
					title: '加载中...',
					success: res => {
						let list = res.data.data.list || [];
						if(page.num == 1) this.dataList = [];
						this.dataList = this.dataList.concat(list);
						this.mescroll.endBySize(list.length, res.data.data.total); 
					},
					fail: () => {
						this.mescroll.endErr()
					}
				});
			},
			reloadList(){
				this.mescroll.resetUpScroll();
			},
			getQueryVariable(url, variable) {
				var vars = url.split('&');
				for (var i = 0; i < vars.length; i++) {
					var pair = vars[i].split('=');
					if (pair[0] == variable) {
						return pair[1];
					}
				}
				return false;
			},
			getShipInfo() {
				this.wxRequest({
					url: `${this.API}/captainApp/captainAuthentication?etpId=${uni.getStorageSync(this.DATA_KEY.etpId)}`,
					method: 'GET',
					success: res => {
						if (res.data.code == 0) {
							this.shipInfo = res.data.data
							if (
								res.data.data.captain.etpId != '' &&
								res.data.data.captain.etpName != '' &&
								!uni.getStorageSync(this.DATA_KEY.etpId) &&
								!uni.getStorageSync(this.DATA_KEY.etpName)
							) {
								uni.setStorageSync(this.DATA_KEY.etpId, res.data.data.captain.etpId);
								uni.setStorageSync(this.DATA_KEY.etpName, res.data.data.captain.etpName);
								this.etpName = res.data.data.captain.etpName
								this.etpId = res.data.data.captain.etpId
							} else if (res.data.data.captain.etpId == '' && res.data.data.captain
								.etpName == '') {
								uni.setStorageSync(this.DATA_KEY.etpId, '');
								uni.setStorageSync(this.DATA_KEY.etpName, '');
							}
						}
					}
				});
			},
		}
	}
</script>

<style>
</style>